import React, { createContext, useState } from 'react'

// 创建 Context 对象
const splitContext = createContext<any>({});

const SplitContextProvider: React.FC<any> = ({ children }) => {
    const [tabItems, setTabItems] = useState<any[]>([]);
    const [tabActiveKey, setTabActiveKey] = useState('');
    // 文件树被选中的节点
    const [treeSeletedKeys, setTreeSeletedKeys] = useState([]);

    // 添加 tabItem，不能重复添加，重复添加就切换 tabActiveKey
    const addTabItem = (data: any) => {
        // console.log(data);
        if (!tabItems.some(item => item.key === data.key)) {
            setTabItems([...tabItems, {
                label: (<span style={{ display: 'flex', alignItems: 'center', gap: '6px' }}>{data.icon} {data.title}</span>),
                key: data.key,
                children: data.title
            }])
        }
        setTabActiveKey(data.key)
    }

    return (
        <splitContext.Provider value={{
            tabItems,
            setTabItems,
            tabActiveKey,
            setTabActiveKey,
            treeSeletedKeys,
            setTreeSeletedKeys,
            addTabItem
        }}>
            {children}
        </splitContext.Provider>
    )
}

export { SplitContextProvider, splitContext } 